package com.tjtt.common.utils;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;
import org.dom4j.io.*;

import com.tjtt.tdm.flow.bean.StepEntity;

public class FlowXmlTool {
    private Document xmldoc;
    
    public FlowXmlTool(String filePath)
    {
    	try{
    		xmldoc = read(filePath);
    	}
    	catch(Exception exp)
    	{
    		exp.printStackTrace();
    	}
    }
    
    public Document read(String fileName) throws MalformedURLException, DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(fileName));
        return document;
     }
    
    /**
     * 根据路径，属性和值获得动作节点
     * @param attr
     * @param value
     * @return
     */
    public Node GetXmlNode(String attr, String value)
    {
    	List  xmlNodeList = xmldoc.selectNodes("//TopFlow/Procs/Proc/BaseProperties");
    	for (Iterator iter = xmlNodeList.iterator(); iter.hasNext(); ) {
    		Node xmlNode = (Node) iter.next();
    		String attrValue = xmlNode.valueOf("@"+attr);
    		if (attrValue.equals(value))
    		{
    			return xmlNode;
    		}
        }
        return null;
    }
    
    public String GetForms()
    {
    	String forms = "";
    	List  xmlNodeList = xmldoc.selectNodes("//TopFlow/Procs/Proc/BaseProperties");
    	int i = 0;
    	for (Iterator iter = xmlNodeList.iterator(); iter.hasNext(); ) {
    		Node xmlNode = (Node) iter.next();
    		String attrValue = xmlNode.valueOf("@form");
    		if (attrValue!=null && !attrValue.equals(""))
    		{
    			String[] attrValues = attrValue.split(",");
    			if (i==0)
    			{
    				forms+=attrValues[0];
    			}
    			else
    			{
    				forms+=","+attrValues[0];
    			}
    			i++;
    		}
        }
    	return forms;
    }

    /**
     * 获得默认路径
     * @param attr
     * @param value
     * @return
     */
    public Node GetXmlStep(String attr, String value)
    {
    	List  xmlNodeList = xmldoc.selectNodes("//TopFlow/Steps/Step/BaseProperties");
    	for (Iterator iter = xmlNodeList.iterator(); iter.hasNext(); ) {
    		Node xmlNode = (Node) iter.next();
    		String attrValue = xmlNode.valueOf("@"+attr);
    		if (attrValue.equals(value))
    		{
    			return xmlNode;
    		}
        }
        return null;
    }
    
    /**
     * 获得子路径地址
     * @return
     */
    public List GetChildFilename()
    {
    	List lists = new ArrayList();
    	List xmlNodeList = xmldoc.selectNodes("//TopFlow/Procs/Proc/BaseProperties");
    	for (Iterator iter = xmlNodeList.iterator(); iter.hasNext(); ) {
    		Node xmlNode = (Node) iter.next();
    		if (xmlNode.valueOf("@childfilename")!="")
    		{
    			lists.add(xmlNode.valueOf("@childfilename"));
    		}
        }
    	return lists;
    }
    
    /**
     * 获得路径列表路径
     * @param attr
     * @param value
     * @return
     */
    public List<StepEntity> GetStepList(String attribute,String value)
    {
    	List<StepEntity> listStepEntity = new ArrayList<StepEntity>();
    	List xmlStepList = xmldoc.selectNodes("//TopFlow/Steps/Step/BaseProperties[@"+attribute+"='"+value+"']");
    	for(Iterator iter = xmlStepList.iterator();iter.hasNext();)
    	{
    		Node node = (Node)iter.next();
//    		if (node.valueOf("@actionType").equals("打回"))
//    		{
//    			continue;
//    		}
    		StepEntity entity = new StepEntity();
    		entity.setId(node.valueOf("@id"));
			entity.setText(node.valueOf("@text"));
			entity.setActionType(node.valueOf("@actionType"));
			entity.setTo(node.valueOf("@to"));
			entity.setFrom(node.valueOf("@from"));
			listStepEntity.add(entity);
    	}
		return listStepEntity;
    }
    
    /**
     * 获得路径列表路径
     * @param attr
     * @param value
     * @return
     */
    public List<StepEntity> GetStepListBack(String attribute,String value)
    {
    	List<StepEntity> listStepEntity = new ArrayList<StepEntity>();
    	List xmlStepList = xmldoc.selectNodes("//TopFlow/Steps/Step/BaseProperties[@"+attribute+"='"+value+"']");
    	for(Iterator iter = xmlStepList.iterator();iter.hasNext();)
    	{
    		Node node = (Node)iter.next();
    		if (node.valueOf("@actionType").equals("顺序"))
    		{
    			continue;
    		}
    		StepEntity entity = new StepEntity();
    		entity.setId(node.valueOf("@id"));
			entity.setText(node.valueOf("@text"));
			entity.setActionType(node.valueOf("@actionType"));
			entity.setTo(node.valueOf("@to"));
			entity.setFrom(node.valueOf("@from"));
			listStepEntity.add(entity);
    	}
		return listStepEntity;
    }

    public String GetParentNode()
    {
    	Node node = xmldoc.selectSingleNode("//TopFlow[1]");
    	if (node!=null)
    	{
    		return node.valueOf("@parentnodeid");
    	}
    	return "";
    }
    
    public List GetFormnodes()
    {
    	List nodelist = xmldoc.selectNodes("//TopFlow/Procs/Proc/BaseProperties[@form!='']");
    	org.dom4j.XPath path =new NumberXPath("@shunxu");  
    	path.sort(nodelist);
    	return nodelist;
    }

}
